<?php
	if(!defined('BASEPATH'))
		exit('No direct script access allowed');
	class Things_Places extends CI_Controller {
		function __construct(){
			session_start();
			parent::__construct();
			}
		
		function index($query_id=0, $order_by  = 'created_date', $sort_order = 'asc', $current_page = 0){
			$search_by='';
			$search_by_value='';
				if($query_id == '0'){		
					//echo '<pre>';print_r($_SESSION);exit;
					unset($_SESSION['search']);
				}else{
					if(isset($_SESSION['search'])){
					$array = $_SESSION['search'];
					//echo '<pre>';print_r($_SESSION);exit;
					$search_by = $array['search_by'];
					$search_by_value = $array['search'];
					}
				}
			
			$results_per_page = 4;
			
			$url = CURL_URL;
			$data = array('www-command'=>'thingsplaces-all_thingstodo','current_page'=>$current_page,'results_per_page'=>$results_per_page,'sort_order'=>$sort_order,'order_by'=>$order_by,'search_by'=>$search_by,'search_by_value'=>$search_by_value);
			$string = http_build_query($data);
			//echo '<pre>';print_r($data);exit;
			$ch = curl_init($url);
			curl_setopt($ch,CURLOPT_POST,true);
			curl_setopt($ch,CURLOPT_POSTFIELDS,$string);
			curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
			$response = json_decode(curl_exec($ch));
			//echo '<pre>';print_r($response);exit;
			curl_close($ch);
			if($response->message !== 'No ThingsPaces'){
			$this->load->library('pagination');			
			$config['base_url'] = 'http://localhost/azindia/azindia-client/index.php/admin/things_places/index/'.$query_id.'/'.$order_by.'/'.$sort_order;
			$config['per_page'] = $results_per_page;
			$config['total_rows']  = $response->total;
			$config['uri_segment'] = 7;
			$config['num_links'] = 1;
			$config['prev_link'] = 'Previous';
			$config['next_link'] = 'Next';
			$config['first_link'] = 'First';
			$config['last_link'] = 'Last';
			$this->pagination->initialize($config);
			$data['pagination'] = $this->pagination->create_links();
			$data['thingsplaces_all_thingstodo'] = $response;
			$data['error'] = '';
			}else{
				$data['error'] = 1;
				$data['thingsplaces_all_thingstodo'] = '';
			}
			//echo '<pre>';print_r($data);exit;
			$data['thingsplaces_fileds'] = array('things_place'=>'Place', 'things_location'=>'Location', 'article_name'=>'Article Type','image'=>'Images','status'=>'Status','created_date'=>'Date');
			$data['order_by'] = $order_by;
			$data['sort_order'] = $sort_order;
			$data['query_id'] = $query_id;
			$data['status'] = array(1=>'Active', 0=>'In-active');
			$data['search_by'] = $search_by;
			$data['search_by_value'] = $search_by_value;
			//echo '<pre>';print_r($response);exit;
			$this->load->model('things_places_model');
			$data['article_list'] = $this->things_places_model->get_list();
			//echo '<pre>';print_r($data);exit;
			$this->load->view('admin/things_places',$data);
		}
		
		function search_by(){
			if($this->input->post('things_search') == ''){
				$search = $this->input->post('article_list');
			}else{
				$search = $this->input->post('things_search');	
			}
				
			$query_array = array(
				'search_by'=>$this->input->post('search_by'),
				'search'=>$search
			);
			$query_id=rand();
			$_SESSION['search']=$query_array;
			//echo '<pre>';print_r($_SESSION['search']);exit;
			redirect('admin/things_places/index/'.$query_id);
			
		}
		
		/* function create_things_places(){
			$this->load->model('things_places_model');
			$data['article_list'] = $this->things_places_model->get_list();
			$this->load->view('admin/create_things_places',$data);
		} */
		
		function add_things_places(){
			$this->form_validation->set_error_delimiters('<span style="color:red">','</span>');
			$this->form_validation->set_rules('article_type','','callback_articletype');
			$this->form_validation->set_rules('place','','required|alpha');
			$this->form_validation->set_rules('location','','required|alpha');
			if($this->form_validation->run() == false){
				
			$url = CURL_URL;
			$data = array('www-command'=>'thingsplaces-get_articles_list');
			$string = http_build_query($data);
			//echo '<pre>';print_r($data);exit;
			$ch = curl_init($url);
			curl_setopt($ch,CURLOPT_POST,true);
			curl_setopt($ch,CURLOPT_POSTFIELDS,$string);
			curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
			$data['article_list'] = json_decode(curl_exec($ch));
			//echo '<pre>';print_r($data['article_list']);exit;
			curl_close($ch);
			$this->load->view('admin/create_things_places',$data);
			}else{
			//$filedata = '@'.$_FILES['image']['tmp_name'][0];
					$filesupload = array('tmp_name'=>$_FILES['image']['tmp_name'],'name'=>$_FILES['image']['name'],'title'=>$this->input->post('image_title'));
					//echo '<pre>';print_r($filesupload);exit;
					/* echo '<pre>';print_r(implode(",",$_FILES['image']['name']));
					echo '<pre>';print_r(implode(",",$this->input->post('image_title')));
					echo '<pre>';print_r(implode(",",$image_encode));exit; */
					
			//echo $image;exit;
			$url = CURL_URL;
			$data = array('www-command'=>'thingsplaces-create_thingstodo','description'=>$this->input->post('content'),'article_id'=>$this->input->post('article_type'),'things_place'=>$this->input->post('place'),'things_location'=>$this->input->post('location'),'things_image'=>$filesupload);
			
			$string = http_build_query($data);
			//$header = array('Content-Type: multipart/form-data');
			//echo '<pre>';print_r($data);exit;
			$ch = curl_init($url);
			curl_setopt($ch,CURLOPT_POST,true);
			curl_setopt($ch, CURLOPT_HTTPHEADER,$header);
			curl_setopt($ch,CURLOPT_POSTFIELDS,$string);
			curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
			$response = curl_exec($ch);
			echo '<pre>';print_r($response);exit;
			curl_close($ch);
			redirect('admin/things_places');
			
			}
		}
		
		function articletype($articletype){
			//echo $articletype;exit;
			if($articletype == ""){
				$this->form_validation->set_message('articletype','Please Select Article type');
				return false;
			}else{
				return true;
			}
		}
		
		function article_type($data){
			if($data == ''){
				$this->form_validation->set_message('article_type','select list');
				return false;
			}else{
				return true;
			}
		}
		
		function edit_things_places(){
			$this->form_validation->set_error_delimiters('<span style="color:red">','</span>');
			$this->form_validation->set_rules('article_type','','callback_articletype');
			$this->form_validation->set_rules('place','','required|alpha');
			$this->form_validation->set_rules('location','','required|alpha');
			if($this->form_validation->run() == false){
				
				$url = CURL_URL;
				$data = array('www-command'=>'thingsplaces-get_articles_list');
				$string = http_build_query($data);
				//echo '<pre>';print_r($data);exit;
				$ch = curl_init($url);
				curl_setopt($ch,CURLOPT_POST,true);
				curl_setopt($ch,CURLOPT_POSTFIELDS,$string);
				curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
				$article_list = json_decode(curl_exec($ch));
				//echo '<pre>';print_r($article_list);exit;
				curl_close($ch);
				
				if($this->uri->segment(4) > 0){
					$_SESSION['thingsid'] = $this->uri->segment(4);
				}
				$data = array('www-command'=>'thingsplaces-get_thingstodo','things_id'=>$_SESSION['thingsid']);
				$string = http_build_query($data);
				//echo '<pre>';print_r($data);exit;
				$ch = curl_init($url);
				curl_setopt($ch,CURLOPT_POST,true);
				curl_setopt($ch,CURLOPT_POSTFIELDS,$string);
				curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
				$get_thingstodo = json_decode(curl_exec($ch));
				
				$data = array('www-command'=>'thingsplaces-get_things_images','things_id'=>$_SESSION['thingsid']);
				$string = http_build_query($data);
				//echo '<pre>';print_r($data);exit;
				$ch = curl_init($url);
				curl_setopt($ch,CURLOPT_POST,true);
				curl_setopt($ch,CURLOPT_POSTFIELDS,$string);
				curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
				$get_things_images = json_decode(curl_exec($ch));
				
				$data['get_thingstodo'] = $get_thingstodo;
				$data['get_things_images'] = $get_things_images;
				$data['article_list'] = $article_list;
				//echo '<pre>';print_r($data);exit;
				curl_close($ch);
				
				$this->load->view('admin/edit_things_places',$data);
			}else{
				//echo '<pre>';print_r($_POST);exit;
				
				$title = $this->input->post('title');
				foreach($_FILES['image']['tmp_name'] as $k=>$test){
					if($test == ''){
						unset($title[$k]);
					}
				}
				//echo '<pre>';print_r($title);exit;
				//echo '<pre>';print_r($_FILES['image']['tmp_name']);exit;
				//echo '<pre>';print_r(count(array_filter($_FILES['image']['tmp_name'])));exit;
				
				$filesupload = array('tmp_name'=>array_filter($_FILES['image']['tmp_name']),'name'=>array_filter($_FILES['image']['name']),'title'=>$title);
				$remove_id = array('remove_id'=>$this->input->post('remove_id'));
				
				$new_image = array_slice($_FILES['image']['name'], 0, count($this->input->post('image_id')));
				$new_image = array_filter($new_image);
				$image_id = array_slice($this->input->post('image_id'), 0, count($new_image));
				//echo '<pre>';print_r($image_id );exit;
			$url = CURL_URL;
			$data = array('www-command'=>'thingsplaces-edit_thingstodo',
							'things_id'=>$_SESSION['thingsid'],
							'description'=>$this->input->post('content'),
							'article_id'=>$this->input->post('article_type'),
							'things_place'=>$this->input->post('place'),
							'things_location'=>$this->input->post('location'),
							'things_image'=>$filesupload,
							'image_id'=>implode(',',$image_id),
							'new_name'=>implode(',',$new_image),
							'remove_id'=>$this->input->post('remove_id')
							);
			
			$string = http_build_query($data);
			//echo '<pre>';print_r($data);exit;
			$ch = curl_init($url);
			curl_setopt($ch,CURLOPT_POST,true);
			curl_setopt($ch,CURLOPT_POSTFIELDS,$string);
			curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
			$response = json_decode(curl_exec($ch));
			//echo '<pre>';print_r($response);exit;
			curl_close($ch);
			redirect('admin/things_places');
			}
		}
		
		function delete_things_places(){
			if($this->uri->segment(5) == 0){
					$status = 1;
				}else{
					$status = 0;
				}
			$url = CURL_URL;
			$data = array('www-command'=>'thingsplaces-delete_thingstodo','things_id'=>$this->uri->segment(4),'status'=>$status);
			
			$string = http_build_query($data);
			//echo '<pre>';print_r($data);exit;
			$ch = curl_init($url);
			curl_setopt($ch,CURLOPT_POST,true);
			curl_setopt($ch,CURLOPT_POSTFIELDS,$string);
			curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
			$response = json_decode(curl_exec($ch));
			//echo '<pre>';print_r($response);exit;
			curl_close($ch);
			redirect('admin/things_places/index');
		}
		
}
?>